<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript" src = "../plugins/bootstrap-table/dist/bootstrap-table.js"></script>
	<script type="text/javascript" src = "../plugins/bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>
	<link rel="stylesheet" type="text/css" href="../plugins/bootstrap-table/dist/bootstrap-table.css">
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/fileinput.js"></script>
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>
	<link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.css">

	<script>
		$(function () {

			loadContent()

			$("#file").fileinput({

				language : "zh",

				uploadUrl : "/fileController/upload",

				maxFileCount: 3,

				maxFileSize: 2048,

				allowedFileExtensions: ["jpg", "png", "gif"],

				enctype : "multipart/form-data"
			})

			$("#file").on("fileuploaded", function (event, data, previewId, index) {

				$("#img_pic").val(data.response.data)
			})

			$("#update_file").fileinput({

				language : "zh",

				uploadUrl : "/fileController/upload",

				maxFileCount: 3,

				maxFileSize: 2048,

				allowedFileExtensions: ["jpg", "png", "gif"],

				enctype : "multipart/form-data",

				initialPreviewAsData: true,

				initialPreviewFileType: "image",

				initialPreview : '<img src = " '+ $("#update_img_pic").val() + '" />'
			})

			$("#update_file").on("fileuploaded", function (event, data, previewId, index) {

				$("#update_img_pic").val(data.response.data)
			})
		})

		function loadContent() {

			$("#dataList").bootstrapTable({

				url : "/contentController/getContentInfo",

				method : "GET",

				cache : false,

				pagination : true,

				sidePagination : "client",

				pageNumber : 1,

				pageSize : 5,

				pageList : [5, 10, 15],

				columns : [

					{

						checkbox : true,

						visible : true
					},

					{

						field : "id",

						title : "广告序号"
					},

					{

						field : "categoryId",

						title : "分类id"
					},

					{

						field : "title",

						title : "标题"
					},

					{

						field : "url",

						title : "链接"
					},

					{

						field : "pic",

						title : "图片",

						formatter : function (value, row, index) {

							var result = ""

							result += '<img src = "' + value + '" width = "100px" height="100px">'

							return result
						}
					},

					{

						field : "sortOrder",

						title : "排序"
					},

					{

						field : "status",

						title : "状态"
					},

					{

						field : "id",

						title : "操作",

						formatter : function (value, row, index) {

							var result = ""

							result += '<a href = "javascript:;" class = "btn btn-xs blue" data-toggle="modal" data-target="#updateModal" onclick = "setContentValue(' + value + ')"><span class = "glyphicon glyphicon-pencil"></span></a>'

							return result
						}
					}
				]
			})
		}

		function setContentValue(obj) {

			$.get("/contentController/getContent?id=" + obj, function (rs) {

				var tbContent = rs.data

				var ops = $("#contentSelect_id").find("option")

				for (var i = 0; i < ops.length; i++) {

					if (ops[i].value == tbContent.categoryId) {

						$(ops[i]).attr("selected", "selected")
					}
				}

				$("[name=title]").val(tbContent.title)

				$("[name=url]").val(tbContent.url)

				$("#update_img_pic").val(tbContent.pic)

				$("[name=sortOrder]").val(tbContent.sortOrder)

				var ras = $("[name=status]")

				for (var i = 0; i < ras.length; i++) {

					if (ras[i].value == tbContent.status) {

						$(ras[i]).attr("checked", "checked")
					}
				}

				$("#update_content_id").val(tbContent.id)

			})
		}

		function deleteContent() {

			var arr = $("#dataList").bootstrapTable("getSelections")

			var ids = []

			for (var i = 0; i < arr.length; i++) {

				ids.push(arr[i].id)
			}

			$.ajax({

				url : "/contentController/deleteContent",

				type : "get",

				dataType : "json",

				data : {ids : ids},

				traditional : true,

				success : function (rs) {

					alert(rs.message)

					$("#dataList").bootstrapTable("refresh")
				}
			})
		}
	</script>
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick = "deleteContent()" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <!--<thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody>
			                          <tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>1</td>
									      <td>促销海报1</td>
									      <td>http://wwww.hb.com/hd1.html</td>
									      <td>
									      	<img alt="" src="" width="100px" height="50px">
									      </td>
									      <td>1</td>
									      <td>有效</td>									     								     
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           
		                                  </td>
			                          </tr>
			                      </tbody>-->
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			
			<form id = "addContent_id">
				<table class="table table-bordered table-striped"  width="800px">
					<tr>
						<td>广告分类</td>
						<td>
							<select class="form-control" name = "categoryId" >
								<option th:each="c : ${cc}" th:value="${c.id}">[[${c.name}]]</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>标题</td>
						<td><input  class="form-control" placeholder="标题" name = "title" >  </td>
					</tr>
					<tr>
						<td>URL</td>
						<td><input  class="form-control" placeholder="URL" name = "url" >  </td>
					</tr>
					<tr>
						<td>排序</td>
						<td><input  class="form-control" placeholder="排序" name = "sortOrder" >  </td>
					</tr>
					<tr>
						<td>广告图片</td>
						<td>
							<table>
								<tr>
									<td>
										<input type="file" id="file" name = "file" />
										<input type = "text" name = "pic" id = "img_pic">
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>是否有效</td>
						<td>
							<input type="radio" name = "status" value = "1" class="icheckbox_square-blue" >是
							<input type="radio" name = "status" value = "0" class="icheckbox_square-blue" >否
						</td>
					</tr>
				</table>
			</form>
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addContent()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <script>

	  function addContent() {

		  $.ajax({

			  url : "/contentController/addContent",

			  type : "get",

			  dataType : "json",

			  data : $("#addContent_id").serialize(),

			  success : function (rs) {

				  alert(rs.message)

				  $("#dataList").bootstrapTable("refresh")
			  }
		  })
	  }

	  function updateContent() {

		  $.ajax({

			  url : "/contentController/updateContent",

			  type : "get",

			  dataType : "json",

			  data : $("#updateContent_id").serialize(),

			  success : function (rs) {

				  alert(rs.message)

				  $("#dataList").bootstrapTable("refresh")
			  }
		  })
	  }
  </script>

  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">广告编辑</h3>
			  </div>
			  <div class="modal-body">

				  <form id = "updateContent_id">
					  <table class="table table-bordered table-striped"  width="800px">
						  <tr>
							  <td>广告分类</td>
							  <td>
								  <input type = "hidden" name = "id" id = "update_content_id">
								  <select class="form-control" name = "categoryId" id = "contentSelect_id" >
									  <option th:each="c : ${cc}" th:value="${c.id}">[[${c.name}]]</option>
								  </select>
							  </td>
						  </tr>
						  <tr>
							  <td>标题</td>
							  <td><input  class="form-control" placeholder="标题" name = "title" >  </td>
						  </tr>
						  <tr>
							  <td>URL</td>
							  <td><input  class="form-control" placeholder="URL" name = "url" >  </td>
						  </tr>
						  <tr>
							  <td>排序</td>
							  <td><input  class="form-control" placeholder="排序" name = "sortOrder" >  </td>
						  </tr>
						  <tr>
							  <td>广告图片</td>
							  <td>
								  <table>
									  <tr>
										  <td>
											  <input type="file" id="update_file" name = "file" />
											  <input type = "text" name = "pic" id = "update_img_pic">
										  </td>
									  </tr>
								  </table>
							  </td>
						  </tr>
						  <tr>
							  <td>是否有效</td>
							  <td>
								  <input type="radio" name = "status" value = "1" class="icheckbox_square-blue" >是
								  <input type="radio" name = "status" value = "0" class="icheckbox_square-blue" >否
							  </td>
						  </tr>
					  </table>
				  </form>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick = "updateContent()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>